﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FxCop.Sdk;

namespace SinacorRules
{
    public sealed class Rule_BovespaExceptionsThrownMustBeInfraException : BaseAddIn.Rules.BaseRuleFXCop
    {
        #region Constructor

        public Rule_BovespaExceptionsThrownMustBeInfraException() :
            base("BovespaExceptionsThrownMustBeInfraException", "SinacorRules.SinacorRules", typeof(Rule_BovespaExceptionsThrownMustBeInfraException).Assembly)
        {
        }

        #endregion

        #region Public Override Methods

        public sealed override ProblemCollection Check(Member member)
        {
            if (member.NodeType == NodeType.Method)
            {
                Method method = member as Method;

                if (method != null)
                {
                    VisitStatements(method.Body.Statements);
                }
            }

            return this.Problems;
        }

        public sealed override void VisitThrow(ThrowNode throwInstruction)
        {
            String exceptionName = throwInstruction.Expression.Type.FullName;

            if (exceptionName != "Sinacor.Infra.Service.ExceptionHandling.BusinessException" &&
                exceptionName != "Sinacor.Infra.Service.ExceptionHandling.ValidationException" &&
                exceptionName != "Sinacor.Infra.Service.ExceptionHandling.GeneralException")
            {
                AddError(throwInstruction, "BovespaExceptionsThrownMustBeInfraExceptionResolution");
            }
        }

        #endregion
    }
}
